package com.ifast.asset.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ifast.asset.domain.LabelDO;
import com.ifast.asset.service.LabelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * <pre>
 *
 * </pre>
 * <small> 2021-03-09 10:55:40 | Aron</small>
 */
@Controller
@RequestMapping("/wxmp/label")
public class LabelController extends BaseController {
    @Autowired
    private LabelService labelService;

    @GetMapping()
        //@RequiresPermissions("wxmp:label:label")
    String Label() {
        return "wxmp/label/label";
    }

    @ResponseBody
    @GetMapping("/list")
    //@RequiresPermissions("wxmp:label:label")
    public Result<Page<LabelDO>> list(LabelDO labelDTO) {
        Wrapper<LabelDO> wrapper = new EntityWrapper<>(labelDTO).orderBy("id", false);
        Page<LabelDO> page = labelService.selectPage(getPage(LabelDO.class), wrapper);
        return Result.ok(page);
    }

    @GetMapping("/add")
        //@RequiresPermissions("wxmp:label:add")
    String add() {
        return "wxmp/label/add";
    }

    @GetMapping("/edit/{id}")
        //@RequiresPermissions("wxmp:label:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        LabelDO label = labelService.selectById(id);
        model.addAttribute("label", label);
        return "wxmp/label/edit";
    }

    @Log("添加")
    @ResponseBody
    @PostMapping("/save")
    //@RequiresPermissions("wxmp:label:add")
    public Result<String> save(@RequestBody LabelDO label) {
        //判断是否存在
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("name", label.getName());
        int count = labelService.selectCount(wrapper);
        if (count == 0) {
            //判断此标签 是否已经存在
            labelService.insert(label);
            return Result.ok();
        } else {
            //已经存在返回提示信息
            return Result.ok("该标签已存在");
        }

    }

    @Log("修改")
    @ResponseBody
    @RequestMapping("/update")
    //@RequiresPermissions("wxmp:label:edit")
    public Result<String> update(LabelDO label) {
        boolean update = labelService.updateById(label);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除")
    @GetMapping("/remove")
    @ResponseBody
    //@RequiresPermissions("wxmp:label:remove")
    public Result<String> remove(Integer id) {
        labelService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除")
    @PostMapping("/batchRemove")
    @ResponseBody
    //@RequiresPermissions("wxmp:label:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Integer[] ids) {
        labelService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

    @ResponseBody
    @GetMapping("/getAll")
    //@RequiresPermissions("wxmp:label:label")
    Result<List<LabelDO>> getAll() {
        List<LabelDO> lists = labelService.selectList(null);
        return Result.ok(lists);
    }
}
